package com.acme;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class PlayerProfile extends Activity {

	private Player player;
	private PlayerProfileResponseReceiver receiver;
	private ListView historyList;
	private ListView accoladeList;
	private View view;
	
	
	public class PlayerProfileResponseReceiver extends BroadcastReceiver {
		   public static final String ACTION_RESP = "com.test.intent.action.PLAYER_PROFILE_RECV";
	
		   private void buildAccoladeLayout(Player player){
			   	ViewGroup parent = (ViewGroup) findViewById(R.id.profile_accolade_layout);

				Iterator<Map.Entry<String,String>> iter = player.yearsByAward.entrySet().iterator();
				 
				while (iter.hasNext()) {
					Map.Entry<String,String> mEntry = iter.next();
			   		view = LayoutInflater.from(getBaseContext()).inflate(R.layout.accolade_item,null);
					TextView heading = (TextView) view.findViewById(R.id.accolade_item_text1);
					TextView details = (TextView) view.findViewById(R.id.accolade_item_text2);
					heading.setText(mEntry.getKey().trim());
					details.setText(mEntry.getValue().trim());
					parent.addView(view);
			   	}
			   	
				
		   }
		   
		   private void buildHistoryLayout(Player player){
		       	ViewGroup parent = (ViewGroup) findViewById(R.id.profile_history_layout);

		        // result: layout_height=wrap_content layout_width=match_parent
		        view = LayoutInflater.from(getBaseContext()).inflate(R.layout.simple_item,null);
		        TextView heading = (TextView) view.findViewById(R.id.simple_item_text1);
		        TextView details = (TextView) view.findViewById(R.id.simple_item_text2);
		        heading.setText("NFL");
		        details.setText(player.getAllTeams());
		        parent.addView(view);
		        
		        view = LayoutInflater.from(getBaseContext()).inflate(R.layout.simple_item,null);
		        heading = (TextView) view.findViewById(R.id.simple_item_text1);
		        details = (TextView) view.findViewById(R.id.simple_item_text2);
		        heading.setText("College");
		        details.setText(player.college);
		        parent.addView(view);
		        
		        view = LayoutInflater.from(getBaseContext()).inflate(R.layout.simple_item,null);
		        heading = (TextView) view.findViewById(R.id.simple_item_text1);
		        details = (TextView) view.findViewById(R.id.simple_item_text2);
		        heading.setText("Draft");
		        details.setText(player.drafted);
		        parent.addView(view);
		   }
		   
		   @Override
		    public void onReceive(Context context, Intent intent) {
		       	String response = intent.getStringExtra(FootballStatsService.GET_PLAYER_OUT_JSON);
		       	player = Player.getPlayer(response);

		       	TextView playerName = (TextView) findViewById(R.id.profile_name);
		       	playerName.setText(player.firstName+" "+player.lastName);
		       	
		       	TextView vitals = (TextView) findViewById(R.id.profile_vitals);
		       	String vitalsStr = 
		       			player.dob + "\n" +
		       			player.hometown + "\n" +
		       			"Height: " + player.height + " Weight: " + player.weight;
		       	vitals.setText(vitalsStr);
		       	
		       	buildHistoryLayout(player);
		       	buildAccoladeLayout(player);
		    }
	}
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.profile);
		
		Intent intent = getIntent();
		String playerId = intent.getStringExtra(Player.PLAYER_ID);
		
		IntentFilter filter = new IntentFilter(PlayerProfileResponseReceiver.ACTION_RESP);
		filter.addCategory(Intent.CATEGORY_DEFAULT);
		receiver = new PlayerProfileResponseReceiver();
		registerReceiver(receiver, filter);

		Intent msgIntent = new Intent(this, FootballStatsService.class);
		msgIntent.putExtra(FootballStatsService.ACTION,FootballStatsService.ACTION_GET_PLAYER);
		msgIntent.putExtra(FootballStatsService.GET_PLAYER_IN_ID, playerId);
		startService(msgIntent);
		
	}
	
	public void onDestroy(){
		super.onDestroy();
		unregisterReceiver(receiver);
	}
}
